package com.kunbo.cn.config;

import com.bstek.uflo.env.EnvironmentProvider;
import com.kunbo.cn.entity.system.user.User;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
 
import javax.persistence.EntityManagerFactory;
import javax.servlet.http.HttpServletRequest;
 
@Configuration
public class EnvironmentProviderImpl implements EnvironmentProvider {
 
    @Autowired
    private SessionFactory sessionFactory;
 
    @Autowired
    private EntityManagerFactory entityManagerFactory;
    @Autowired  
    private  HttpServletRequest request;
 
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
 
    public PlatformTransactionManager getPlatformTransactionManager() {
        return new JpaTransactionManager(entityManagerFactory);
    }
 
    public String getCategoryId() {
        return null;
    }
    public String getLoginUser() {
    	User user = (User) request.getSession().getAttribute("user");
    	if(null==user) {
    		user = (User) request.getSession().getAttribute("userApp");
    	}
    	if(null==user) {
    		return "当前用户没有登录";
    	}else {
    		System.out.println("当前登录用户为："+user.getName());
    		return user.getName();
    	}
    }
}
